﻿@using FwProject.Settlement.Dto
@using X.PagedList
@model IPagedList<ReceiptFastlyDto>
@{
    ViewBag.Title = "快速收款";
}
@section styles{
    <link href="~/Theme/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Theme/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <style type="text/css">
        .input-group[class*=col-] {
            padding: 0 15px;
        }
    </style>
}
<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>@ViewBag.Title</h5>
                <div class="ibox-tools">
                    @if (IsGranted(FwProject.Authorization.PermissionNames.Finance_Operation_AddPaid))
                    {
                        <button class="btn btn-primary btn-xs" id="btnSave">批量保存</button>
                    }
                    else
                    {
                        <button type="button" class="btn btn-default btn-xs">无收款权限</button>
                    }

                </div>
            </div>
            <div class="ibox-content">
                <div class="row m-b-sm m-t-sm">
                    <form class="form-horizontal" id="form" asp-controller="Receipt" asp-action="FastlyReceipt" data-ajax="true" data-ajax-update="#data_list" data-ajax-method="Get" data-ajax-mode="replace">
                        <input type="hidden" id="size" name="size" value="0" />
                        <div class="col-lg-2 col-md-2">
                            <label class="control-label col-lg-4 col-md-5">餐厅：</label>
                            <div class="input-group">
                                <input type="text" name="RestaurnatName" id="RestaurnatName" placeholder="请输入餐厅名称" class="input-sm form-control">
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-2">
                            <label class="control-label col-lg-5 col-md-5">结算方式：</label>
                            <div class="input-group">
                                <select class="form-control" name="BalanceWay">
                                    <option value="" selected="selected">全部</option>
                                    <option value="0">日结</option>
                                    <option value="1">定期结算</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-2 col-lg-2">
                            <label class="control-label col-md-4 col-lg-4">线路：</label>
                            <div class="input-group">
                                @Html.DropDownList("Line", ViewBag.Line as IEnumerable<SelectListItem>, "全部", new { @class = "form-control", id = "Line" })
                            </div>
                        </div>
                        <div class="col-md-3 col-lg-2">
                            <label class="control-label col-md-4 col-lg-4">司机：</label>
                            <div class="input-group">
                                <input type="hidden" name="DriverId" id="DriverId" />
                                <input type="text" id="dropdown-driverid" class="form-control" onblur="blurEmpty()" placeholder="全部">
                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown" style="line-height:1.455;padding:6px 12px;">
                                        <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-right" role="menu" style="z-index: 9999;"></ul>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-3">
                            <label class="control-label col-lg-5 col-md-4">结算周期：</label>
                            <div class="input-group">
                                <select class="form-control" name="BalanceCycle">
                                    <option value="" selected="selected">全部</option>
                                    <option value="0">每周定期</option>
                                    <option value="1">半月定期</option>
                                    <option value="2">每月定期</option>
                                    <option value="3">自定日期</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-1">
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-sm btn-primary"> 查询</button>
                            </span>
                        </div>
                    </form>
                </div>
                <div id="data_list">
                    @{Html.RenderPartial("FastlyReceiptPartial", Model);}
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/Theme/js/content.min.js?v=1.0.0"></script>
    <script src="~/Theme/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Theme/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="~/Theme/js/plugins/suggest/bootstrap-suggest2014.js"></script>
    <script src="~/js/DateComm.js"></script>
    <script src="~/js/PrintUtil.js"></script>
    <script type="text/javascript">

        function setPrice(obj) {
            if ($(obj).val() == "") {
                $(obj).val(0);
            }
            var tr = $(obj).parent().parent();
            var TotalPrice = parseFloat($(tr).find("td input").eq(2).val());//应收金额

            if (TotalPrice < 0) {
                $(tr).find("td input").eq(5).val(0.00);
                $(tr).find("td input").eq(6).val(0.00);
            }
            var PrePayment = parseFloat($(tr).find("td input").eq(3).val());//预收款余额
            var TaskBackAmount = parseFloat($(tr).find("td input").eq(4).val());//本次收款
            var UsePrePayment = parseFloat($(tr).find("td input").eq(5).val());//本次使用预收款
            var PreferentialAmount = parseFloat($(tr).find("td input").eq(6).val());//优惠金额

            if (UsePrePayment > PrePayment) {
                $(tr).find("td input").eq(5).val(0);
                UsePrePayment = 0;
            }
            if (TaskBackAmount + UsePrePayment + PreferentialAmount > TotalPrice) {
                if (TaskBackAmount > TotalPrice) {//本次收款大于应收
                    TaskBackAmount = TotalPrice;
                    UsePrePayment = 0.00;
                    PreferentialAmount = 0.00;
                    $(tr).find("td input").eq(4).val(TaskBackAmount);//本次收款等于应收
                    $(tr).find("td input").eq(5).val(UsePrePayment);
                    $(tr).find("td input").eq(6).val(PreferentialAmount);
                }
                else if (TaskBackAmount + UsePrePayment > TotalPrice) {//本次收款加使用预收款大于应收
                    UsePrePayment = toFloat(TotalPrice - TaskBackAmount);
                    PreferentialAmount = 0.00;
                    $(tr).find("td input").eq(5).val(UsePrePayment);
                    $(tr).find("td input").eq(6).val(PreferentialAmount);
                }
                else {
                    PreferentialAmount = toFloat(TotalPrice - TaskBackAmount - UsePrePayment);
                    $(tr).find("td input").eq(6).val(PreferentialAmount);
                }
            } else {
                if ($(obj).data("name") != "preferentialAmount") {
                    PreferentialAmount = toFloat(TotalPrice - TaskBackAmount - UsePrePayment);
                    $(tr).find("td input").eq(6).val(PreferentialAmount);
                }

            }

            //if (TaskBackAmount < TotalPrice) {
            //    $(tr).find("td input").eq(6).val(toFloat(TotalPrice - TaskBackAmount - UsePrePayment));
            //} else {
            //    $(tr).find("td input").eq(6).val(0);
            //}
        }



        var toFloat = function (value) {
            value = Math.round(parseFloat(value) * 100) / 100;
            if (value.toString().indexOf(".") < 0) {
                value = value.toString() + ".00";
            }
            return value;
        }

        function Print() {
            printGirdObj({
                containerId: 'table_print',//包含表格容器のID
                title: '快速收款',
                showRowNum: false,  //表格中rownumbers: true时为true
                showOrder: false,//是否显示序号，rownumbers: true时有效，默认false
                printInit: { //打印初始化设置参数
                    portrait: false //true纵向,false横向
                }
            });
        }
        $("#btnSave").on("click", function () {
            layer.msg('保存中请稍后！', {
                icon: 1,
                time: 4000
            });
            $("#form1").submit();
        })

        //layer.msg("test",{time:2000});
        $(document).ready(function () {
            $('#modal_ajax').on('show.bs.modal', function (e) {
                $(this).addClass('loading');
                var $button = $(e.relatedTarget);
                var url = $button.data('url');
                if (!!url) {
                    url = url + ((url.indexOf("?") > -1) ? "&r=" : "?r=") + Math.random();
                    $(this).load(url, function () {
                        $.validator.unobtrusive.parse(this); //$("#form_model_create").validate();
                        $("input[name='CreationTime']").datepicker({ todayBtn: "linked", keyboardNavigation: !1, forceParse: !1, calendarWeeks: !0, autoclose: !0 });
                        $(".i-checks").iCheck({ checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green", });
                        $(this).removeClass('loading');
                    });
                }
            });
            $.getJSON("/Users/GetSuggestData?type=司机类型").done(function (data) {
                CommonAjaxCallback(data, function () {
                    bindSuggest(data.result);
                }, null);
            }).fail(function () {
                layer.msg('连接超时，获取司机信息失败！', {
                    icon: 2,
                    time: 2000
                });
            });
            //开始时间
            $('#beginTime').datepicker({
                todayBtn: "linked",
                autoclose: true,
                todayHighlight: true

            }).on('changeDate', function (e) {
                var startTime = e.date;
                $('#endTime').datepicker('setStartDate', startTime);
            });
            //结束时间：
            $('#endTime').datepicker({
                todayBtn: "linked",
                autoclose: true,
                todayHighlight: true

            }).on('changeDate', function (e) {
                var endTime = e.date;
                $('#beginTime').datepicker('setEndDate', endTime);
            });
        });
        function bindSuggest(data) {
            if ($("#dropdown-driverid") != undefined) {
                $role = $("#dropdown-driverid").bsSuggest({
                    effectiveFields: ["name"],
                    effectiveFieldsAlias: { "name": "姓名" },
                    searchFields: ["name"],
                    clearable: true,
                    getDataMethod: "data",
                    //url: "/Users/GetSuggestData?type=司机类型&keyword=",
                    data: { value: data },
                    idField: "id",
                    keyField: "name",
                    callfun: function () {
                        $("#DriverId").val("");
                    }
                }).on('onDataRequestSuccess', function (e, result) {
                    console.log('onDataRequestSuccess: ', result);
                }).on('onSetSelectValue', function (e, keyword, data) {
                    $("#DriverId").val(data.id);
                    console.log('onSetSelectValue: ', keyword, data);
                }).on('onUnsetSelectValue', function () {
                    console.log('onUnsetSelectValue');
                });
            }
        }
        function blurEmpty() {
            var cityObj = $("#dropdown-driverid");
            var DriverId = $("#DriverId");
            if (cityObj.val() == "") {
                DriverId.attr("value", "");
            }
        }
        function ReloadAjaxCallbackgoback(data, successInfo) {
            CommonAjaxCallback(data, function () {
                window.location.reload();
            }, successInfo);
        }

        new DateContUtil({//【下拉日期】
            selectId: "data_select",//下拉框のID
            startDate: 'beginTime',//开始日期(不填默认search_startDateBase)
            endDate: 'endTime',//结束日期(不填默认search_endDateBase)
            defaultValue: '4',//本月
            selectedFun: function () {//选中后触发の方法
                $(".form-horizontal").submit();
            }
        });

        $("#data_list").on("change", "#pageSize", function () {
            layer.load();
            var size = $("option:selected", this).val();
            var pageurl = $("#pageurl").val();
            $("#data_list").load("/Receipt/FastlyReceipt?size=" + size + "&r=" + Math.random() + pageurl, function () {
                $("#size").val($("option:selected", this).val());
                layer.closeAll("loading");
            });
        });
    </script>
}
